<!--
 * @name: 
 * @Author: JiaYing
 * @LastEditors: JiaYing
 * @Date: 2022-08-21 16:07:59
 * @LastEditTime: 2022-08-21 17:16:31
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>var和let 区别</title>
  </head>
  <body></body>
</html>
<script>
  console.log(a); // undefined ，变量提升
  var a = 1;
  console.log(window.a); // 1 , 全局变量

  try {
    function fn() {
      var b = 2;
      a = 3;
    }
    fn();
    console.log(a, b); // 3，ReferenceError: b is not defined
  } catch (e) {
    console.log(e);
  }

  try {
    console.log(c); // 暂时性死区
    // ReferenceError: Cannot access 'c' before initialization
    let c = 1;
  } catch (e) {
    console.log(e);
  }

  // let 块级作用域
  {
    var d = 1;
    let e = 1;
  }
  try {
    console.log(d); // 1
    console.log(e); // ReferenceError: e is not defined
  } catch (e) {
    console.log(e);
  }

  // 声明相同变量名
  try {
    var f = 1;
    var f = 2;
    console.log(f);
    // 编译出错
    // Uncaught SyntaxError: Identifier 'g' has already been declared
    // let g = 1;
    // let g = 2;
    // console.log(g);
  } catch (e) {
    console.log(e);
  }
</script>
